package com.vip.jwt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vip.jwt.common.exception.MyException;
import com.vip.jwt.common.utils.menu.MenuTreeUtil;
import com.vip.jwt.entity.Menu;
import com.vip.jwt.entity.RoleMenu;
import com.vip.jwt.mapper.MenuMapper;
import com.vip.jwt.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vip.jwt.service.RoleMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author vip
 * @since 2021-05-04
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private RoleMenuService roleMenuService;

    @Override
    public int deleteById(String id) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<Menu> list = menuMapper.selectList(queryWrapper);
        if(list.size()>0){
            throw new MyException("请先删除子节点");
        }
        menuMapper.deleteById(id);
        return 0;
    }

    @Override
    public List<com.vip.jwt.common.utils.menu.Menu> tree() {
        List<Menu> menus = menuMapper.selectList(null);
        List<com.vip.jwt.common.utils.menu.Menu> menuList =  menus.stream()
                .map(v->new com.vip.jwt.common.utils.menu.Menu(v.getId(),v.getTitle(),v.getUrl(),v.getSort(),v.getIcon(),v.getParentId()))
                .collect(Collectors.toList());
        return new MenuTreeUtil(menuList).buildTree();
    }

    @Override
    public List<com.vip.jwt.common.utils.menu.Menu> tree(List<String> roleIds) {
        List<String> menuIds = roleMenuService.findByRoleIds(roleIds);
        List<Menu> roleMenus = new ArrayList<>();
        List<Menu> menus = menuMapper.selectList(null);
        for(int i=0;i<menus.size();i++){
            Menu menu = menus.get(i);
            for(int j=0;j<menuIds.size();j++){
                if(menu.getId().equals(menuIds.get(j))){
                    roleMenus.add(menu);
                    break;
                }
            }
        }
        List<Menu> finalMenuList = new ArrayList<>();
        for(int i=0;i<roleMenus.size();i++){
            Menu menu = roleMenus.get(i);
            String menuId = menu.getId();
            List<Menu> findMenus = RoleMenu.findById(menus, menuId);
            finalMenuList.addAll(findMenus);
        }
        //去重
        finalMenuList = finalMenuList.stream().distinct().collect(Collectors.toList());
        List<com.vip.jwt.common.utils.menu.Menu> menuList =  finalMenuList.stream()
                .map(v->new com.vip.jwt.common.utils.menu.Menu(v.getId(),v.getTitle(),v.getUrl(),v.getSort(),v.getIcon(),v.getParentId()))
                .collect(Collectors.toList());
        return new MenuTreeUtil(menuList).buildTree();
    }

}
